package org.rexsong.example.quartzsample.config;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.rexsong.example.quartzsample.domain.Jobs;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;

@Configuration
public class JobsConfig {

    @Value("classpath:jobs.json")
    Resource resourceFile;

    private List<Jobs> jobsList;

    @PostConstruct
    public void init() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        jobsList = objectMapper.readValue(resourceFile.getFile(), new TypeReference<List<Jobs>>() {});
    }

    @Bean(name = "jobsList")
    public List<Jobs> jobConfigs() {
        return jobsList;
    }
}
